﻿using CompanyManagementDLL.MetaData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CompanyManagementDLL.DLL
{
    public class ProjectDLL
    {
        private readonly CompanyMgtEntities db = new CompanyMgtEntities();
        #region Fecth Data
        /// <summary>
        /// Return project detail based in project id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ProjectMetaData GetProjectByID(int id)
        {
            ProjectMetaData projectmetadata = new ProjectMetaData();
            try
            {
                projectmetadata = db.Projects.Where(r => r.PorjectID == id).Select(u => new ProjectMetaData
                {
                    ProjectName = u.ProjectName,
                    PorjectID = u.PorjectID,
                    StartDate = u.StartDate,
                    EndDate = u.EndDate,
                    CreatedBy = u.CreatedBy,
                    ModifyBy = u.ModifyBy,
                    ModifyDate = u.ModifyDate,
                    CreatedDate = u.CreatedDate,
                    IsActive = u.IsActive ?? false,
                    IsArchive = u.IsArchive,
                    Description = u.Description
                }).FirstOrDefault();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return projectmetadata;
        }

        #endregion

        #region Get List

        /// <summary>
        /// Retrun project list
        /// </summary>
        /// <returns></returns>
        public List<ProjectMetaData> GetProductList()
        {
            List<ProjectMetaData> ProjectMetaData = new List<ProjectMetaData>();
            try
            {
                ProjectMetaData = db.Projects.Where(u => u.IsActive == true && u.IsArchive == false).Select(u => new ProjectMetaData
                {
                    ProjectName = u.ProjectName,
                    PorjectID = u.PorjectID,
                    StartDate = u.StartDate,
                    EndDate = u.EndDate,
                    CreatedBy = u.CreatedBy,
                    ModifyBy = u.ModifyBy,
                    ModifyDate = u.ModifyDate,
                    CreatedDate = u.CreatedDate,
                    IsActive = u.IsActive ?? false,
                    IsArchive = u.IsArchive,
                    Description = u.Description
                }).ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ProjectMetaData;
        }

        /// <summary>
        /// Return project list based on UserID
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public List<ProjectMetaData> getProjectListByUserID(int UserID)
        {
            List<ProjectMetaData> list = new List<ProjectMetaData>();
            try
            {
                list = db.GetProjectListByUserID(UserID).Select(u => new ProjectMetaData
                {
                    ProjectName = u.ProjectName,
                    PorjectID = u.PorjectID,
                    StartDate = u.StartDate,
                    EndDate = u.EndDate,
                    CreatedBy = u.CreatedBy,
                    ModifyBy = u.ModifyBy,
                    ModifyDate = u.ModifyDate,
                    CreatedDate = u.CreatedDate,
                    IsActive = u.IsActive ?? false,
                    IsArchive = u.IsArchive,
                    Description = u.Description,
                    UserName = u.UserName,
                    CreatedUser = u.CreatedUser
                }).ToList();
            }
            catch (Exception ex)
            {

                throw ex;
            }
            return list;
        }


        #endregion

        #region DML Opration

        /// <summary>
        /// insert user entity
        /// </summary>
        /// <param name="ProjectMetaData"></param>
        public int InsertProject(ProjectMetaData ProjectMetaData)
        {
            Project tblProject = new Project();
            try
            {
                tblProject.ProjectName = ProjectMetaData.ProjectName;
                tblProject.StartDate = ProjectMetaData.StartDate;
                tblProject.EndDate = ProjectMetaData.EndDate;
                tblProject.Description = ProjectMetaData.Description;
                tblProject.IsActive = ProjectMetaData.IsActive;
                tblProject.IsArchive = false;
                tblProject.CreatedBy = ProjectMetaData.CreatedBy;
                tblProject.CreatedDate = DateTime.Now;
                db.Projects.Add(tblProject);
                db.SaveChanges();
            }
            catch (Exception ex)
            {

                throw ex;
            }
            return tblProject.PorjectID;
        }

        /// <summary>
        /// update project entity
        /// </summary>
        /// <param name="ProjectMetaData"></param>
        public void UpdateProject(ProjectMetaData ProjectMetaData)
        {
            Project tblProject = new Project();
            try
            {
                tblProject = db.Projects.Where(p => p.PorjectID == ProjectMetaData.PorjectID).FirstOrDefault();
                tblProject.ProjectName = ProjectMetaData.ProjectName;
                tblProject.StartDate = ProjectMetaData.StartDate;
                tblProject.EndDate = ProjectMetaData.EndDate;
                tblProject.Description = ProjectMetaData.Description;
                tblProject.IsActive = ProjectMetaData.IsActive;
                tblProject.ModifyBy = ProjectMetaData.ModifyBy;
                tblProject.ModifyDate = DateTime.Now;
                db.SaveChanges();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// delete user entity
        /// </summary>
        /// <param name="id"></param>
        public void DeleteProject(int id)
        {
            try
            {
                Project tblProject = new Project();
                tblProject = db.Projects.Where(r => r.PorjectID == id).FirstOrDefault();
                tblProject.IsArchive = true;
                db.SaveChanges();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        #endregion
    }
}
